import json
from ui import SocialNetwork


class Student:
    def __init__(self, username, fullName, age, school, interests):
        self.username = username
        self.fullName = fullName
        self.age = age
        self.school = school
        self.interests = interests
        self.friends = []

    def add_friend(self, friend):
        if friend not in self.friends:
            self.friends.append(friend)

    def get_friends(self):
        return self.friends

    def filter_friends(self, interest):
        return [friend for friend in self.friends if interest in friend.interests]


class Community:
    def __init__(self):
        self.members = []

    def add_member(self, student):
        if student not in self.members:
            self.members.append(student)

    def get_members(self):
        return self.members


def find_bridges(communities):
    bridges = []
    for student in [member for community in communities for member in community.get_members()]:
        community_count = 0
        for community in communities:
            if student in community.get_members():
                community_count += 1
        if community_count > 1:
            bridges.append(student)
    return bridges


def load_students(file_path):
    try:
        with open(file_path, 'r') as file:
            data = json.load(file)
            students = []
            for student_data in data["students"]:
                student = Student(
                    student_data["username"],
                    student_data["fullName"],
                    student_data["age"],
                    student_data["school"],
                    student_data["interests"]
                )
                students.append(student)
            return students
    except FileNotFoundError:
        print(f"Document {file_path} can't find.")
    except json.JSONDecodeError:
        print(f"Can't anyasis the json date of {file_path}.")
    return []


if __name__ == "__main__":
    # 加载学生数据
    students = load_students("students.json")
    community1 = Community()
    community2 = Community()
    # 启动 GUI
    app = SocialNetwork(students, [community1, community2])
    app.run()